<?php namespace App\Admin\Goods\JmThemeGoods;

//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\PaginateCriteria;
use App\Core\Repository\Criterias\SortCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

use Input;
use DB;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\All;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    use \App\Core\Service\Services\Sorter;


    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        //添加观察者
        $this->addListeners([
            'create' => new Listeners\CreateListener(),
            'update' => new Listeners\UpdateListener()
        ]);
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            new InputTimeRangeCriteria('created_at'),
            new Criterias\MatchSchedule()
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $conditions[] = new SortCriteria('sort', 'ASC');
        $conditions[] = new SortCriteria('id', 'DESC');
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        $data->load('goods','jmtheme');
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }


    //排序
    public function sort(){
        $this->initSorter([
            'attrs'=>['theme_id']
        ]);
        return $this->applySort();
    }


    //批量添加商品
    public function multiChangeCategory(){
        $goodsIdsRaw = Input::get('goods_ids', '');
        $themeIdsRaw = Input::get('theme_ids');
        $goodsIds = array_filter(explode(',', $goodsIdsRaw), 'is_numeric');

        $goods = DB::table('jimiao_theme_goods')
            ->where('theme_id','=',$themeIdsRaw)
            ->where('is_delete','=','0')
            ->get();

        $arrGoods = array();
        foreach ($goods as $goodsIndex){
            $arrGoods[] = $goodsIndex->goods_id;
        }

        $result=array_intersect($goodsIds,$arrGoods);
        $data=[];
        foreach($goodsIds as $k=>$v){
            $flag = false; //是否存在
            foreach ($result as $k1=>$v1){ //遍历交集
                if($v1 == $v){ //判断商品id 是否在交集中
                    $flag = true;
                    break;
                }
            }
            if($flag){ //存在，continue
                continue;
            }
            $data[$k]['goods_id']=$v;
            $data[$k]['theme_id']=$themeIdsRaw;
            $data[$k]['created_at']=time();
            $data[$k]['updated_at']=time();
        }

        $res = DB::table('jimiao_theme_goods')->insert($data);
        if(!$res){
            return $this->inputError('非法操作');
        }
        return $this->success();
    }


}